using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the Cash table. It enables loading from a file,
    /// saving to a file and more.
    /// </summary>
    public partial class CashEditor : UserControl
    {
        public DSIPAMS.DTCashDataTable cash;
        string[] cashHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="CashEditor"/> class.
        /// </summary>
        public CashEditor()
        {
            InitializeComponent();
            InitializeComponentExtneded();
        }

        /// <summary>
        /// Initializes the component extneded.
        /// </summary>
        private void InitializeComponentExtneded()
        {
            cash = new DSIPAMS.DTCashDataTable();
            dgvCash.DataSource = cash;
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvCash control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvCash_DataSourceChanged(object sender, EventArgs e)
        {
            dgvCash.Refresh();
        }

        /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns> a DataTable with the file's data</returns>
        public DataTable LoadData(string dataFilePath)
        {

            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                cashHeaders = sr.ReadLine().Split(',');
               
                while (true)
                {
                    string singleCash = sr.ReadLine();
                    if (singleCash == string.Empty || singleCash == null) break;
                    string[] singleCashInfo = singleCash.Split(',');
                    if (singleCashInfo.Length != 3) continue;
                    CultureInfo ci = new CultureInfo("he-IL");
                    cash.AddDTCashRow(singleCashInfo[0], Convert.ToDouble(singleCashInfo[1]),
                                      DateTime.Parse(singleCashInfo[2], ci.DateTimeFormat));
                }
                sr.Close();
                Text = dataFilePath;

                cash.AcceptChanges();
                dgvCash.DataSource = cash;
                for (int index = 0; index < cashHeaders.Length; index++)
                {
                    dgvCash.Columns[index].HeaderText = cashHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvCash.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }
        
            return cash;
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = cashHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTCashRow singleCash in cash)
            {
                fileContent += string.Format("{0},{1},{2}" + Environment.NewLine, singleCash[0], singleCash[1], singleCash[2]);
            }
            sw.Write(fileContent);
            sw.Close();

        }

   
    }
    
}
